package com.enterprise.support.utility;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-7-11
 * Time: 21:21:47
 * To change this template use File | Settings | File Templates.
 */
public class Enterprise<K, T> extends LinkedHashMap<K, T> implements Serializable {

    /*页大小*/
    public final static String PAGE_SIZE = "enterprise_page_size";
    /*总页数*/
    public final static String PAGE_COUNT = "enterprise_page_count";
    /*总记录数*/
    public final static String PAGE_RESULT_COUNT = "enterprise_page_result_count";
    /*当前索引页数*/
    public final static String PAGE_INDEX = "enterprise_page_index";
    /*结果*/
    public final static String PAGE_RESULT = "enterprise_page_result";
    /*显示几页*/
    public final static String PAGE_INDEX_COUNT = "enterprise_page_index_count";
    /*排序*/
    public final static String PAGE_ORDER_BY = "enterprise_page_order_by";
    /*错误字段*/
    private Enterprise errorFieldMessages = null;
    protected String enterprisename;

    public Enterprise() {
    }

    public Enterprise(String name) {
        this.enterprisename = name;
    }

    public Enterprise(Map entity) {
        super(entity);
    }

    public Enterprise(Enterprise entity) {
        super(entity);
    }


    public Enterprise(String enterprisename, Enterprise entity) {
        this.enterprisename = enterprisename;
        super.putAll(entity);
    }


    public Long getLongId() {
        return getLong("id");
    }

    public void setLongId(Integer id) {
        setLong("id", id);
    }

    public Integer getIntId() {
        return getInt("id");
    }

    public void setIntId(Integer id) {
        setInt("id", id);
    }

    public Object set(Object key, Object value) {
        return super.put((K) key, (T) value);
    }

    public void setString(Object key, String value) {
        set(key, value);
    }

    public void setInt(Object key, Integer value) {
        set(key, value);
    }

    public void setDouble(Object key, double value) {
        set(key, value);
    }

    public void setFloat(Object key, float value) {
        set(key, value);
    }

    public void setLong(Object key, long value) {
        set(key, value);
    }

    public void setShort(Object key, Short value) {
        set(key, value);
    }

    public void setBoolean(Object key, boolean value) {
        set(key, value);
    }

    public void setBigDecimal(Object key, BigDecimal value) {
        set(key, value);
    }

    public BigDecimal getBigDecimal(Object key) {
        Object o = get(key);
        if (o == null) return new BigDecimal(0.0D);
        else return (BigDecimal) o;
    }

    public Integer getInt(Object key) {
        Object o = get(key);
        if (o == null) {
            return 0;
        }
        Class classType = o.getClass();
        if (classType == java.lang.Integer.class) {
            return ((Integer) o).intValue();
        } else if (classType == java.lang.Short.class) {
            return new Integer(((Short) o).shortValue());
        } else if (classType == java.lang.Long.class) {
            try {
                return ((Long) o).intValue();
            } catch (Exception ex) {
                return 0;
            }
        } else if (classType == java.lang.String.class) {
            try {
                return Integer.parseInt(o.toString());
            } catch (Exception ex) {
                return 0;
            }
        }
        return 0;
    }

    public double getDouble(Object key) {
        Object o = get(key);
        if (o == null) return 0;
        Class classType = o.getClass();
        if (classType == java.lang.Double.class) {
            return ((Double) o).doubleValue();
        } else if (classType == java.lang.Float.class) {
            try {
                return (double) ((Float) o).floatValue();
            } catch (Exception e) {
                return 0;
            }
        } else if (classType == java.lang.String.class || classType == java.math.BigDecimal.class) {
            try {
                return Double.parseDouble(o.toString());
            } catch (Exception e) {
                return 0;
            }
        } else {
            return 0;
        }
    }

    public float getFloat(Object key) {
        Object o = get(key);
        if (o == null) return 0;
        Class classType = o.getClass();
        if (classType == java.lang.Float.class) {
            return ((Float) o).floatValue();
        } else if (classType == java.lang.String.class || classType == java.math.BigDecimal.class) {
            try {
                return Float.parseFloat(o.toString());
            } catch (Exception e) {
                return 0;
            }
        } else {
            return 0;
        }
    }

    public Long getLong(Object key) {
        Object o = get(key);
        if (o == null) return 0l;
        Class classType = o.getClass();
        if (classType == java.lang.Long.class) {
            return ((Long) o).longValue();
        } else if (classType == java.lang.Integer.class) {
            return (long) ((Integer) o).intValue();
        } else if (classType == java.lang.Short.class) {
            return (long) ((Short) o).shortValue();
        } else if (classType == java.lang.String.class) {
            return Long.parseLong(o.toString());
        } else {
            return 0l;
        }
    }

    public short getShort(Object key) {
        Object o = get(key);
        if (o == null) return 0;
        Class classType = o.getClass();
        if (classType == java.lang.Short.class) {
            return ((Short) o).shortValue();
        } else if (classType == java.lang.String.class) {
            try {
                return Short.parseShort(o.toString());
            } catch (Exception e) {
                return 0;
            }
        } else {
            return 0;
        }
    }

    public boolean getBoolean(Object key) {
        Object o = get(key);
        if (o == null) return false;
        if (o.getClass().isInstance(new Boolean(true))) {
            return ((Boolean) o).booleanValue();
        } else if (o.getClass().isInstance(new String())) {
            try {
                return Boolean.getBoolean(o.toString());
            } catch (Exception e) {
                return false;
            }
        } else {
            return false;
        }
    }

    public String getString(Object key) {
        Object o = null;
        try {
            o = get(key);
            return o == null ? null : o.toString();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            return "EException:" + ex.getMessage();
        } catch (Exception ex) {
            ex.printStackTrace();
            return "EException:" + ex.getMessage();
        }
    }

    /*获得类的基本信息*/

    public String toString() {
        return toString(1);
    }

    public String toString(Integer i) {
        if (i > 2) return "Warn ...StackOverflowError:" + this.hashCode();
        StringBuffer buf = new StringBuffer();
        Set fileds = super.keySet();
        buf.append("\n\t-----------[ Enterprise Result ]---------------------");
        buf.append("\n\t\t    KEY\t\t\t|\t VALUE");
        buf.append("\n\t-----------------------------------------------------");
        for (Object field : fileds) {
            int fieldLength = field.toString().length();
            Object value = get(field);
            if (value != null) {
                if (value instanceof Enterprise) {
                    value = ((Enterprise) value).toString(++i);
                }
            }
            buf.append("\n\t  " + field);
            for (int t = 0; t < 5 - (Math.max(1, (fieldLength / 4) + ((fieldLength % 4) > 2 ? 1 : 0))); t++) {
                buf.append("\t");
            }
            buf.append("|    " + value);
        }
        return buf.append("\n\t-----------------------------------------------------").toString();
    }

    public Enterprise getEnterprise(String key) {
        return (Enterprise) get(key);
    }

    public void setMessage(String message) {
        setString("message", message);
    }

    public String getMessage() {
        return getString("message");
    }

    public String getEnterpriseName() {
        return this.enterprisename;
    }

    public void setEnterpriseName(String name) {
        this.enterprisename = name;
    }


    /*传入参数 指定的字段是否为空*/

    public boolean isEmpty(String field) {
        return Validation.isEmpty(get(field));
    }

    public boolean isEmpty(String field, String eq) {
        return Validation.isEmpty(get(field), eq);
    }


    /*比较*/

    public boolean equals(String key, Object tvalue) {
        return isEmpty(key) ? false : (get(key).toString().equalsIgnoreCase(tvalue.toString()));
    }

    /*获取默认值*/

    public Object defaults(String field, Object value) {
        if (isEmpty(field)) {
            return value;
        }
        return get(field);
    }

    public Date getStartDate() {
        Date startDate = DataFormat.StringFormtDate(getString("startDate"));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public Date getEndDate() {
        Date endDate = DataFormat.StringFormtDate(getString("endDate"));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endDate);
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    public void setOrderBy(String orderBy) {
        setString(PAGE_ORDER_BY, orderBy);
    }


    public boolean equals(Object entity) {
        if (entity instanceof Enterprise) {
            Enterprise enter = (Enterprise) entity;
            return !enter.isEmpty("id") && !this.isEmpty("id") && enter.getIntId() == this.getIntId();
        }
        return false;
    }

    public String getCachedate() {
        return getString("cachedate");
    }

    public void setCachedate(String cachedate) {
        setString("cachedate", cachedate);
    }

    public int hashCode() {
        return isEmpty("id") ? System.identityHashCode(this) : get("id").hashCode();
    }

    public static void main(String[] args) {
        Enterprise enter = new Enterprise();
        enter.setString("username", "asdfasdfasdf");
        enter.setString("hometowncity", "asdfasdfasdf");
        enter.setString("address", "asdfasdfasdf");
        enter.setString("se", "asdfasdfasdf");
        enter.setString("signature", "asdfasdfasdf");
        System.out.println(enter);
        //System.out.println(JSONObject.fromObject(new Enterprise()));
    }

    public void setErrorField(String key, String value) {
        if (errorFieldMessages == null) {
            errorFieldMessages = new Enterprise();
        }
        errorFieldMessages.setString(key, value);
    }

    public String getErrorField(String key) {
        return (errorFieldMessages == null || errorFieldMessages.isEmpty(key)) ? "" : errorFieldMessages.getString(key);
    }
}
